package com.sz.controller;

import com.sz.pojo.RevExp;
import com.sz.pojo.User;
import com.sz.service.PlanSVImpl;
import com.sz.service.RevExpSVImpl;
import com.sz.service.UserSVImpl;
import com.sz.service.VerfySVImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.AbstractQueue;
import java.util.Date;

@Controller
@RequestMapping("/update")
public class ModifyController {
    @Resource
    private UserSVImpl userSV;
    @Resource
    private RevExpSVImpl revExpSV;
    @Resource
    private PlanSVImpl planSV;
    @Resource
    private VerfySVImpl verfySV;

    @RequestMapping("/name")
    public String updateName(@RequestParam("name") String name, HttpServletRequest req, Model model){
        String username = (String) req.getSession().getAttribute("un");
        int i = userSV.updateName(username, name);
        model.addAttribute("i",i);
        return "forward:/toIndex2";
    }

    @RequestMapping("/password")
    public String updatePassword(@RequestParam("password") String password, HttpServletRequest req, Model model){
        String username = (String) req.getSession().getAttribute("un");
        int i = userSV.updatePassword(username, password);
        model.addAttribute("i",i);
        return "forward:/toIndex2";
    }

    @RequestMapping("/remarks")
    public String updateRemarks(@RequestParam("remarks") String remarks, HttpServletRequest req, Model model){
        String username = (String) req.getSession().getAttribute("un");
        int i = userSV.updateRemarks(username, remarks);
        model.addAttribute("i",i);
        return "forward:/toIndex2";
    }

    @RequestMapping("/toEdit")
    public String toEdit(int id,Model model){
        RevExp revExp = revExpSV.queryById(id);
        model.addAttribute("revExp",revExp);
        return "edit";
    }

    @RequestMapping("/updateRevexp")
    public String updateRevExp(RevExp revExp,HttpServletRequest req){
        int i = revExpSV.updateById1(revExp);
        int auth = (Integer)req.getSession().getAttribute("auth");
        if(auth==0) return "redirect:/query/all0";
        else return "redirect:/query/all2";
    }

    @RequestMapping("/thisyear")
    public String updateThisyear(String planContent){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        int year =Integer.valueOf(sdf.format(date));
        int i = planSV.modifyThisyear(planContent, year);
        return "redirect:/toIndex2";
    }

    @RequestMapping("/nextyear")
    public String updateNextyear(String planContent){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        int year =Integer.valueOf(sdf.format(date));
        int i = planSV.modifyNextyear(planContent,year);
        return "redirect:/toIndex2";
    }

    @RequestMapping("/user")
    public String updateUser(User user,HttpServletRequest req,Model model){
        String authmsg = verfySV.isAuthTrue(user.getAuthority(), req);
        if (authmsg!="") model.addAttribute("authmsg",authmsg);
        else userSV.updateOneById(user);
        return "forward:/toIndex3";
    }
}
